﻿using Mate.Caching;
using Mate.Core.Enums;
using Mate.Db.Model.Comment;
using Mate.Db.Model.Good;
using Mate.Db.Model.Merchant;
using Mate.Db.Model.Users;
using Mate.Db.SqlSugar;
using Mate.Dto.Comment;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mate.Comment.Service
{
    public class CommentService : DataService<mp_comment>, ICommentService
    {
        private readonly ICacheManager _cache;
        public CommentService(DataClient dataClient, ICacheManager cache) : base(dataClient)
        {
            this._cache = cache;
        }

        #region 获取点评实体
        /// <summary>
        /// 获取点评实体
        /// </summary>
        /// <param name="id">点评id</param>
        /// <param name="comment_type">点评类型，10商户，20商品  Mate.Core.Enums.CommentTypeEnum</param>
        /// <returns></returns>
        public CommentModel GetComment(int id, int comment_type)
        {
            CommentModel model = new CommentModel();
            if (comment_type == CommentTypeEnum.Merchant.GetHashCode())
            {
                model = dataClient.Queryable<mp_comment, mp_user, mp_comment_tag, mp_merchant>((c, u, ct, m) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    JoinType.Left,c.comment_tag==ct.id,
                    JoinType.Left,c.relateid==m.id
                    })
                  .Where((c, u, ct, m) => c.id == id && c.is_delete == 1 && c.comment_type == comment_type)
                  .Select<CommentModel>("c.*,u.username,ct.tagname,m.merchantname relatename")
                  .First();
            }
            else if (comment_type == CommentTypeEnum.Good.GetHashCode())
            {
                model = dataClient.Queryable<mp_comment, mp_user, mp_comment_tag, mp_good>((c, u, ct, g) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    JoinType.Left,c.comment_tag==ct.id,
                    JoinType.Left,c.relateid==g.id
                    })
                  .Where((c, u, ct, m) => c.id == id && c.is_delete == 1 && c.comment_type == comment_type)
                  .Select<CommentModel>("c.*,u.username,ct.tagname,g.name relatename")
                  .First();
            }
            return model;

        }
        #endregion

        #region 获取点评列表
        /// <summary>
        /// 获取点评列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="comment_type">点评类型，10商户，20商品  Mate.Core.Enums.CommentTypeEnum</param>
        /// <param name="key">搜索关键字</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public IList<CommentModel> GetCommentList(int pageIndex, int pageSize, int comment_type, string key, ref int totalCount)
        {
            string likecount = "(select count(1) from mp_like where userid=c.userid and relateid=c.id and liketype=1 and is_delete=1) likecount";
            string replycount = "(select count(1) from mp_reply where userid=c.userid and commentid=c.id and is_delete=1) replycount";

            IList<CommentModel> list = new List<CommentModel>();
            if (comment_type == CommentTypeEnum.Merchant.GetHashCode())
            {
                list = dataClient.Queryable<mp_comment, mp_user, mp_comment_tag, mp_merchant>((c, u, ct, m) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    JoinType.Left,c.comment_tag==ct.id,
                    JoinType.Left,c.relateid==m.id
                    })
                    .Where((c, u, ct, m) => c.is_delete == 1 && c.comment_type == comment_type)
                    .WhereIF(!string.IsNullOrWhiteSpace(key), c => c.title.Contains(key) || c.content.Contains(key))
                    .Select<CommentModel>($"c.*,u.username,ct.tagname,m.merchantname relatename,{likecount},{replycount}")
                    .ToPageList(pageIndex, pageSize, ref totalCount);
            }
            else if (comment_type == CommentTypeEnum.Good.GetHashCode())
            {
                list = dataClient.Queryable<mp_comment, mp_user, mp_comment_tag, mp_good>((c, u, ct, g) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    JoinType.Left,c.comment_tag==ct.id,
                    JoinType.Left,c.relateid==g.id
                    })
                    .Where((c, u, ct, m) => c.is_delete == 1 && c.comment_type == comment_type)
                    .WhereIF(!string.IsNullOrWhiteSpace(key), c => c.title.Contains(key) || c.content.Contains(key))
                    .Select<CommentModel>($"c.*,u.username,ct.tagname,g.name relatename,{likecount},{replycount}")
                    .ToPageList(pageIndex, pageSize, ref totalCount);
            }
            return list;
        }
        #endregion

        #region 获取首页点评列表
        /// <summary>
        /// 获取首页点评列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="merchant_category_id">商户类型</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public IList<HomeCommentModel> GetHomeCommentList(int pageIndex, int pageSize, IEnumerable<int> merchant_category_id, ref int totalCount)
        {
            int comment_type = CommentTypeEnum.Merchant.GetHashCode();
            string likecount = "(select count(1) from mp_like where userid=c.userid and relateid=c.id and liketype=1 and is_delete=1) likecount";
            return dataClient.Queryable<mp_comment, mp_user, mp_merchant>((c, u, m) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    JoinType.Left,c.relateid==m.id
                    })
                   .Where((c, u, m) => c.is_delete == 1 && c.comment_type == comment_type && merchant_category_id.Contains(m.merchantcategory))
                   .OrderBy((c, u, m) => c.id, OrderByType.Desc)
                   .Select<HomeCommentModel>($"c.id,m.id merchant_id,c.img_one comment_img,c.title,u.usericon user_avator,u.id user_id,u.nickname user_nickname,{likecount}")
                   .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        #endregion

        #region 获取商户点评列表
        /// <summary>
        /// 获取商户点评列表
        /// </summary>
        /// <param name="merchantId">商户id</param>
        /// <returns></returns>
        public IList<MerchantCommentDto> GetMerchantCommentList(int pageIndex, int pageSize, int merchantId, ref int totalCount)
        {
            int comment_type = CommentTypeEnum.Merchant.GetHashCode();
            return dataClient.Queryable<mp_comment, mp_user>((c, u) => new object[]{
                    JoinType.Left,c.userid==u.id,
                    })
                   .Where((c, u) => c.is_delete == 1 && c.comment_type == comment_type && c.relateid == merchantId)
                   .OrderBy((c, u) => c.id, OrderByType.Desc)
                   .Select((c, u) => new MerchantCommentDto()
                   {
                       id = c.id,
                       merchant_id = c.relateid,
                       user_id = c.userid,
                       user_nickname = u.nickname,
                       user_avator = u.usericon,
                       createtime = c.createtime,
                       title = c.title,
                       content = c.content,
                       img_one = c.img_one,
                       img_two = c.img_two,
                       img_three = c.img_three,
                       img_four = c.img_four,
                       img_five = c.img_five
                   })
                   .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        #endregion

    }
}
